/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    console.log(id);
    sendAjax('/v1/teacher/program/detail.json', 'get', {id:id}, function(json) {
        var program = json.body.data;
        console.log(program);
        $('.program-title').text(program.title);
        $('.apply-start-end-time').text(program.apply_start_at+' ~ '+program.apply_end_at);
        $('.check-start-end-time').text(program.check_start_at+' ~ '+program.check_end_at);
        var state = getProgramState(program.state);
        $('.program-status').text(state);
        $('.program-desc').text(program.desc);
        var select = json.body.select;
        $('.program-user-status').val(select.status);
        var tip = '';
        if (select.status == 2) {
            $('.program-user-state').val(select.state);
            tip = '<span style="color:#1890FF;">您已得到选房资格，请于 '+select.start_at+' ～ '+select.end_at+' 选房</span>'
            $('.program-user-tip').append(tip);
        }
        if (select.status == 3) {
            tip = '<span style="color:#F5222D;">非常遗憾，您未已得到选房资格，下次继续努力！</span>';
            $('.program-user-tip').append(tip);
        }
        console.log(id);
        loadList('/v1/teacher/program/'+id+'/apart/list.json');
    });

    $('#view-select-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var apart_id = button.data('apart-id');
        var apart_name = button.data('apart-name');
        $(this).find('.apart-id').val(apart_id);
        $(this).find('.apart-name').text(apart_name);
    });
    $('#view-select-dialog').on('hide.bs.modal', function(){
        $(this).find('.apart-id').val('');
        $(this).find('.apart-name').text('');
    });

    $('#view-select-dialog .btn-save').on('click', function(){
        var apart_id = $('#view-select-dialog').find('.apart-id').val();
        sendAjax('/v1/teacher/select/apart', 'post', {program_id:id, apart_id:apart_id}, function(json){
            $('#view-select-dialog').modal('hide');
            $('#action-tip .tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#view-release-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var apart_id = button.data('apart-id');
        var apart_name = button.data('apart-name');
        $(this).find('.user-apart-id').val(id);
        $(this).find('.apart-id').val(apart_id);
        $(this).find('.apart-name').text(apart_name);
    });
    $('#view-release-dialog').on('hide.bs.modal', function(){
        $(this).find('.apart-id').val('');
        $(this).find('.apart-name').text('');
    });

    $('#view-release-dialog .btn-save').on('click', function(){
        var apart_id = $('#view-release-dialog').find('.apart-id').val();
        sendAjax('/v1/teacher/release/apart', 'post', {apart_id:apart_id}, function(json){
            $('#view-release-dialog').modal('hide');
            $('#action-tip .tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        location.reload();
    });
});
function loadList(url) {
    $("#apart-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'apart_id',
            title: '公寓ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'region',
            title: '公寓所在地'
        }, {
            field: 'area',
            title: '面积',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '30方';
                    case 2 : return '60方';
                    case 3 : return '90方';
                    case 4 : return '120方';
                    case 5 : return '120方以上';
                    default: return '30方';
                }
            }
        }, {
            field: 'rent',
            title: '月租金',
            formatter: function (value) {
                return '¥ '+value;
            }
        }, {
            field: 'other',
            title: '其它信息'
        }, {
            field: 'is_selected',
            title: '是否已选择',
            formatter: function (value) {
                return value == 1 ? '是' : '否';
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                return row.is_selected == 1 ? '<a class="btn btn-xs btn-primary view-pay" data-toggle="modal" data-target="#view-release-dialog" data-id="'+value+'" data-apart-name="'+row.name+'" data-apart-id="'+row.apart_id+'">取消</a>' : '<a class="btn btn-xs btn-primary view-pay" data-toggle="modal" data-target="#view-select-dialog" data-apart-name="'+row.name+'" data-apart-id="'+row.apart_id+'">选择</a>';
            }
        }]
    });
}

function deleteRepair(url, id, callback) {
    $.post(url, {id:id}, function(e){
        var json = JSON.parse(e);
        callback(json);
    });
}

function getProgramState(state) {
    switch (state) {
        case 1 : return '申请中';
        case 2 : return '选房中';
        case 3 : return '已完成';
        default: return '申请中';
    }
}